﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using IndoorPosition.Server.Core.Entities;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IndoorPosition.Server.Application
{
    public class ConfigurationService : IConfigurationService, ITransient
    {

        IRepository<Configuration> _configRepository;

        public ConfigurationService(IRepository<Configuration> configRepository)
        {
            _configRepository = configRepository;
        }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task AddAsync(ConfigurationInputDto input)
        {
            var exist = await _configRepository.Where(c => c.Key == input.Key).AnyAsync();
            if (exist)
            {
                throw new Exception("已存在相同关键字的参数");
            }
            await _configRepository.InsertNowAsync(input.Adapt<Configuration>());
        }

        /// <summary>
        /// 添加或更新参数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task AddOrUpdateAsync(ConfigurationInputDto input)
        {
            var config = await _configRepository.Where(c => c.Key == input.Key).FirstOrDefaultAsync();
            if (config != null)
            {
                config.Value = input.Value;
                config.Name = input.Name;
                await _configRepository.UpdateNowAsync(config);
            }
            else
            {
                await _configRepository.InsertNowAsync(input.Adapt<Configuration>());
            }
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<ConfigurationDto> GetAsync(string key)
        {
            var config = await _configRepository.Where(c => c.Key == key).FirstOrDefaultAsync();
            return config.Adapt<ConfigurationDto>();
        }

        /// <summary>
        /// 获取所有参数列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<ConfigurationDto>> ListAsync()
        {
            var configs = await _configRepository.AsQueryable().ToListAsync();
            return configs.Adapt<List<ConfigurationDto>>();
        }

        /// <summary>
        /// 更新参数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateAsync(ConfigurationDto input)
        { 
            await _configRepository.UpdateNowAsync(input.Adapt<Configuration>());
        }

        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _configRepository.DeleteNowAsync(id);
        }
    }
}
